home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / demos / OpenGL / globe / globe.c++ < prev    next >
C/C++ Source or Header  |  1996-11-11  |  5KB  |  231 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. #include <stdio.h>
  9. #include <unistd.h>
  10.  
  11. #include <GL/gl.h>
  12. #include <GL/glu.h>
  13. #include <GL/glut.h>
  14.  
  15. #include <Inventor/SoDB.h>
  16. #include <Inventor/nodes/SoComplexity.h>
  17. #include <Inventor/nodes/SoFont.h>
  18. #include <Inventor/nodes/SoGroup.h>
  19. #include <Inventor/nodes/SoSeparator.h>
  20. #include <Inventor/nodes/SoSphere.h>
  21. #include <Inventor/nodes/SoText2.h>
  22. #include <Inventor/nodes/SoTexture2.h>
  23. #include <Inventor/nodes/SoTranslation.h>
  24.  
  25. #include <Inventor/nodes/SoTexture2Transform.h>
  26.  
  27. #include <Inventor/SoInput.h>
  28. #include <Inventor/SbViewportRegion.h>
  29. #include <Inventor/nodes/SoSeparator.h>
  30. #include <Inventor/actions/SoGLRenderAction.h>
  31. #include <Inventor/nodes/SoCylinder.h>
  32. #include <Inventor/nodes/SoDirectionalLight.h>
  33. #include <Inventor/nodes/SoEventCallback.h>
  34. #include <Inventor/nodes/SoMaterial.h>
  35. #include <Inventor/nodes/SoPerspectiveCamera.h>
  36. #include <Inventor/nodes/SoRotationXYZ.h>
  37. #include <Inventor/nodes/SoTransform.h>
  38. #include <Inventor/nodes/SoTranslation.h>
  39.  
  40. int W = 300, H = 300;
  41. int spinning = 0;
  42. GLubyte *image = NULL;
  43. SoSeparator *root;
  44. SoRotationXYZ *globeSpin;
  45. float angle = 0.0;
  46. int moving  = 0;
  47. int begin;
  48.  
  49. void
  50. reshape(int w, int h)
  51. {
  52.   glViewport(0, 0, w, h);
  53.   W = w;
  54.   H = h;
  55.   if (image)
  56.     free(image);
  57.   image = (GLubyte *) malloc(W * H * 3);
  58. }
  59.  
  60. void
  61. renderScene(void)
  62. {
  63.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  64.   SbViewportRegion myViewport(W, H);
  65.   SoGLRenderAction myRenderAction(myViewport);
  66.   myRenderAction.apply(root);
  67. }
  68.  
  69. void
  70. redraw(void)
  71. {
  72.   renderScene();
  73.   glutSwapBuffers();
  74. }
  75.  
  76. void
  77. globeScene(void)
  78. {
  79.    root = new SoSeparator;
  80.    root->ref();
  81.  
  82.    // Add a camera and light
  83.    SoPerspectiveCamera *myCamera = new SoPerspectiveCamera;
  84.    myCamera->position.setValue(0., 0., 2.2);
  85.    myCamera->heightAngle = M_PI/2.5; 
  86.    myCamera->nearDistance = 0.5;
  87.    myCamera->farDistance = 10.0;
  88.    root->addChild(myCamera);
  89.    root->addChild(new SoDirectionalLight);
  90.  
  91.    SoRotationXYZ *globalRotXYZ = new SoRotationXYZ;
  92.    globalRotXYZ->axis = SoRotationXYZ::X;
  93.    globalRotXYZ->angle = M_PI/9;
  94.    root->addChild(globalRotXYZ);
  95.  
  96.    // Set up the globe transformations
  97.    globeSpin = new SoRotationXYZ;
  98.    root->addChild(globeSpin);
  99.    globeSpin->angle = angle;
  100.    globeSpin->axis = SoRotationXYZ::Y;  // rotate about Y axis
  101.  
  102.    // Add the globe, a sphere with a texture map.
  103.    // Put it within a separator.
  104.    SoSeparator *sphereSep = new SoSeparator;
  105.    SoTexture2  *myTexture2 = new SoTexture2;
  106.    SoComplexity *sphereComplexity = new SoComplexity;
  107.    sphereComplexity->value = 0.55;
  108.    root->addChild(sphereSep);
  109.    sphereSep->addChild(myTexture2);
  110.    sphereSep->addChild(sphereComplexity);
  111.    sphereSep->addChild(new SoSphere);
  112.    myTexture2->filename = "globe.rgb";
  113. }
  114.  
  115. void
  116. updateModels(void)
  117. {
  118.   globeSpin->angle = angle;
  119.   glutPostRedisplay();
  120. }
  121.  
  122. void
  123. animate(void)
  124. {
  125.   angle += 0.1;
  126.   updateModels();
  127. }
  128.  
  129. void
  130. setAnimation(int enable)
  131. {
  132.   if(enable) {
  133.     spinning = 1;
  134.     glutIdleFunc(animate);
  135.   } else {
  136.     spinning = 0;
  137.     glutIdleFunc(NULL);
  138.     glutPostRedisplay();
  139.   }
  140. }
  141.  
  142. /* ARGSUSED */
  143. void
  144. keyboard(unsigned char ch, int x, int y)
  145. {
  146.   if(ch == ' ') {
  147.     setAnimation(0);
  148.     animate();
  149.   }
  150. }
  151.  
  152. void
  153. menuSelect(int item)
  154. {
  155.    switch(item) {
  156.    case 1:
  157.      animate();
  158.      break;
  159.    case 2:
  160.       if(!spinning) {
  161.             setAnimation(1);
  162.        } else {
  163.             setAnimation(0);
  164.        }
  165.       break;
  166.    }
  167. }
  168.  
  169. void
  170. vis(int visible)
  171. {
  172.   if (visible == GLUT_VISIBLE) {
  173.     if (spinning)
  174.       glutIdleFunc(animate);
  175.   } else {
  176.     if (spinning)
  177.       glutIdleFunc(NULL);
  178.   }
  179. }
  180.  
  181. /* ARGSUSED */
  182. void
  183. mouse(int button, int state, int x, int y)
  184. {
  185.   if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  186.     setAnimation(0);
  187.     moving = 1;
  188.     begin = x;
  189.   }
  190.   if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
  191.     moving = 0;
  192.     glutPostRedisplay();
  193.   }
  194. }
  195.  
  196. /* ARGSUSED */
  197. void
  198. motion(int x, int y)
  199. {
  200.   if (moving) {
  201.     angle = angle + .01 * (x - begin);
  202.     begin = x;
  203.     updateModels();
  204.   }
  205. }
  206.  
  207. void
  208. main(int argc, char **argv)
  209. {
  210.   glutInit(&argc, argv);
  211.   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
  212.  
  213.   SoDB::init();
  214.   globeScene();
  215.  
  216.   glutInitWindowSize(W, H);
  217.   glutCreateWindow("As the world turns");
  218.   glutDisplayFunc(redraw);
  219.   glutReshapeFunc(reshape);
  220.   glutCreateMenu(menuSelect);
  221.   glutAddMenuEntry("Step", 1);
  222.   glutAddMenuEntry("Toggle spinning", 2);
  223.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  224.   glutKeyboardFunc(keyboard);
  225.   glutMouseFunc(mouse);
  226.   glutMotionFunc(motion);
  227.   glutVisibilityFunc(vis);
  228.  
  229.   glutMainLoop();
  230. }
  231.